package com.issue.system.controller;

import com.issue.common.annotation.Log;
import com.issue.common.core.controller.BaseController;
import com.issue.common.core.domain.AjaxResult;
import com.issue.common.core.domain.entity.SysUser;
import com.issue.common.core.page.TableDataInfo;
import com.issue.common.enums.BusinessType;
import com.issue.common.utils.poi.ExcelUtil;
import com.issue.system.domain.AChildInfo;
import com.issue.system.domain.ChildCourse;
import com.issue.system.domain.ChildQuiz;
import com.issue.system.mapper.AChildInfoMapper;
import com.issue.system.mapper.ChildCourseMapper;
import com.issue.system.mapper.ChildQuizMapper;
import com.issue.system.mapper.SysUserMapper;
import com.issue.system.service.IChildQuizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * QuizController
 * 
 * @author system
 * @date 2024-10-20
 */
@RestController
@RequestMapping("/system/quiz")
public class ChildQuizController extends BaseController
{
    @Autowired
    private IChildQuizService childQuizService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private AChildInfoMapper childInfoMapper;

    @Resource
    private ChildQuizMapper childQuizMapper;

    @Resource
    private ChildCourseMapper childCourseMapper;

    /**
     * 查询Quiz列表
     */
    @PreAuthorize("@ss.hasPermi('system:quiz:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChildQuiz childQuiz)
    {
        startPage();
        List<ChildQuiz> list = childQuizService.selectChildQuizList(childQuiz);
        return getDataTable(list);
    }

    /**
     * 查询Quiz列表
     */
    @PreAuthorize("@ss.hasPermi('system:quiz:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(@RequestParam("phone") String phone,
                                 @RequestParam(value = "courseId", required = false) Long courseId,
                                 @RequestParam(value = "childId", required = false) Long childId)
    {
        List<ChildQuiz> list = childQuizMapper.selectListByPhone(phone, courseId, childId);
        return getDataTable(list);
    }

    /**
     * 导出Quiz列表
     */
    @PreAuthorize("@ss.hasPermi('system:quiz:export')")
    @Log(title = "Quiz", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChildQuiz childQuiz)
    {
        List<ChildQuiz> list = childQuizService.selectChildQuizList(childQuiz);
        ExcelUtil<ChildQuiz> util = new ExcelUtil<ChildQuiz>(ChildQuiz.class);
        util.exportExcel(response, list, "Quiz数据");
    }

    /**
     * 获取Quiz详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:quiz:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(childQuizService.selectChildQuizById(id));
    }

    /**
     * 新增Quiz
     */
    @PreAuthorize("@ss.hasPermi('system:quiz:add')")
    @Log(title = "Quiz", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChildQuiz childQuiz)
    {
        Long teacherId = childQuiz.getTeacherId();
        Long childId = childQuiz.getChildId();
        Long courseId = childQuiz.getCourseId();

        if (teacherId == null || childId == null) {
            return AjaxResult.error("Error Message");
        }

        ChildCourse childCourse = childCourseMapper.selectChildCourseByCourseId(courseId);
        if (childCourse == null){
            return AjaxResult.error("Error Course");
        }

        SysUser teacher = sysUserMapper.selectUserById(teacherId);
        if (teacher == null){
            return AjaxResult.error("Error Teacher");
        }
        childQuiz.setTeacherName(teacher.getUserName());

        AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
        if (childInfo == null){
            return AjaxResult.error("Error Child");
        }
        childQuiz.setChildCardNo(childInfo.getChildCardNo());
        childQuiz.setChildName(childInfo.getChildName());
        childQuiz.setCourseId(childCourse.getCourseId());
        childQuiz.setCourseName(childCourse.getCourseName());

        return toAjax(childQuizService.insertChildQuiz(childQuiz));
    }

    /**
     * 修改Quiz
     */
    @PreAuthorize("@ss.hasPermi('system:quiz:edit')")
    @Log(title = "Quiz", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChildQuiz childQuiz)
    {

        Long teacherId = childQuiz.getTeacherId();
        Long childId = childQuiz.getChildId();

        if (teacherId != null) {
            SysUser teacher = sysUserMapper.selectUserById(teacherId);
            if (teacher != null){
                childQuiz.setTeacherName(teacher.getUserName());
            }
        }

        if (childId != null) {
            AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
            if (childInfo != null){
                childQuiz.setChildCardNo(childInfo.getChildCardNo());
                childQuiz.setChildName(childInfo.getChildName());
            }
        }

        return toAjax(childQuizService.updateChildQuiz(childQuiz));
    }

    /**
     * 删除Quiz
     */
    @PreAuthorize("@ss.hasPermi('system:quiz:remove')")
    @Log(title = "Quiz", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(childQuizService.deleteChildQuizByIds(ids));
    }
}
